import os
from launch import LaunchDescription
from launch.actions import ExecuteProcess
from launch_ros.actions.node import Node
from launch_ros.substitutions import FindPackageShare

def generate_launch_description():
    robot_name_in_modle = 'fishbot'
    pkg_name = 'fishbot_description'
    urdf_name = 'fishbot_gazebo.urdf'

    ld = LaunchDescription()
    pkg_name = FindPackageShare(package=pkg_name).find(pkg_name)
    urdf_model_path = os.path.join(pkg_name,f'urdf/{urdf_name}')

    world_path = os.path.join(pkg_name,'world/fishbot.world')

    gazebo_cmd = ExecuteProcess(
        cmd=['gazebo','--verbose','-s','libgazebo_ros_init.so','-s','libgazebo_ros_factory.so',world_path],
        output='screen'
    )

    spawn_entity_cmd = Node(
       package='gazebo_ros',
       executable='spawn_entity.py',
       arguments=['-entity',robot_name_in_modle,'-file',urdf_model_path],
       output='screen'
    )

    # start robot state publisher
    start_robot_state_publisher_cmd = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        arguments=[urdf_model_path]
    )
    # rviz2 
    start_rviz2_cmd = Node(
        package='rviz2',
        executable='rviz2',
        name='rviz2',
        output='screen'
    )
    ld.add_action(gazebo_cmd)
    ld.add_action(spawn_entity_cmd)
    ld.add_action(start_robot_state_publisher_cmd)
    ld.add_action(start_rviz2_cmd)
    return ld
